<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>添加/编辑证照</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-row>
            <el-col :span="18">
              <el-form-item label="证照名称:" prop="name">
                <el-input v-model="editForm.name" auto-complete="off" placeholder="填写证照名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="证照管理人：" prop="num">
                <el-button @click.native="getMoreTeach()" size="small">选择证照管理人（单人）</el-button>
                <el-tag
                    closable
                    :disable-transitions="false"
                    @close="handleCloseExamine()"
                    class="mr-10"
                    v-if="examine_user.realname"
                >{{ examine_user.realname }}
                </el-tag>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="抄送人：" prop="num">
                <el-button @click="getMoreTeachCopy()" size="small">选择证照抄送人</el-button>
                <el-tag
                    closable
                    :disable-transitions="false"
                    @close="handleCloseExamineCopy(line)"
                    class="mr-10"
                    size="small"
                    v-for="(items,line) in dynamicTags"
                    :key="items.id"
                >{{ items.realname }}
                </el-tag>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small" >返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定</el-button>
      </div>
    </div>
    <!--    单选教职工审核人-->
    <singleTeacherModle :visible.sync="singleTeachVisible" @studentFn="studentFn"
                        :dynamicTagsArray="examine_user"></singleTeacherModle>
    <MultipleTeacherModle :visible.sync="studentVisible" @studentFn="studentFnCopy"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
  </div>
</template>

<script>

export default {
  props: ['visible','licenceId'], // commoent 学生点评 collection 学生采集
  components: {
  },
  data() {
    return {
      singleTeachVisible:false,
      dialogVisible: false,
      examine_user: {}, // 审核人
      editForm: {
        name: '', // 名称
        user_id: '' // 审核人
      }, // 编辑界面数据
      dynamicTagsArray: {},
      studentVisible: false,
      dynamicTags: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if(this.visible){
        if(this.licenceId){
          this.getDetail();
        }
      }else{
        for(let i in this.editForm){
          this.editForm[i] = '';
        }
        this.examine_user = {}
        this.dynamicTags = []
        this.dynamicTagsArray = {}
      }
    }
  },
  created() {
  },
  methods: {
    getMoreTeachCopy(){
      let a_ = {}
      for(let item of this.dynamicTags){
        a_[item.id] = item
      }
      this.dynamicTagsArray = JSON.parse(JSON.stringify(a_))
      this.studentVisible = true
    },
    studentFnCopy(option) {
      let optionData = []
      for (let i in option) {
        optionData.push(option[i])
      }
      this.dynamicTags = JSON.parse(JSON.stringify(optionData))
    },
    handleCloseExamineCopy(index){
      this.dynamicTags.splice(index, 1)
    },
    // 打开审核人
    getMoreTeach() {
      this.singleTeachVisible = true;
    },
    // 删除审核人
    handleCloseExamine() {
      this.examine_user = {}
    },
    // 审核人回执
    studentFn(option) {
      this.examine_user_tag = [];
      this.examine_user = {};
      this.examine_user_tag.push(option);
      this.examine_user = this.getJson(option);
      this.editForm.user_id = option.id;
      console.log('提交的参数：', this.editOption)
    },
    getDetail () {
      this.$fetch(this.api.licence.edit, {id: this.licenceId}).then((response) => {
        if (!response.code) {
          this.editForm.name = response.vo.name;
          this.editForm.user_id = response.vo.user?response.vo.user.id: '';
          this.examine_user = this.getJson(response.vo.user)
          this.dynamicTags = this.getJson(response.vo.read_user)
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option){
      return JSON.parse(JSON.stringify(option))
    },
    // 提交数据
    async editSubmit () {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if(!dataOption.name){
        this.$message.error('填写证照名称')
        return
      }
      if(!dataOption.user_id){
        this.$message.error('选择证照管理人')
        return
      }
      let a_ = []
      for(let item of this.dynamicTags){
        a_.push(item.id)
      }
      dataOption.read_user = a_.join(',')
      let url = this.api.licence.add
      if (this.licenceId) {
        url = this.api.licence.edit
        dataOption.id = this.licenceId
      }
      console.log('url:', dataOption)
      let data = await this.$post(url, dataOption);
      if (!data.code) {
        this.$message.success('编辑成功！')
        this.$emit('InitFn')
        this.closePOpup()
      } else {
        this.$message.error(data.msg)
      }
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}
.hearder-search{
  display: flex;
  align-items: center;
}
</style>


